package com.xie.system.infrastructure.security.authorization;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.xie.security.base.BaseJwtUser;
import com.xie.security.base.extension.DummyJwtUser;
import com.xie.security.base.extension.UserContextAware;
import com.xie.security.config.AuthErrorMsg;
import com.xie.security.exception.CustomAuthenticationException;
import com.xie.system.domain.admin.service.IAdminUserService;
import com.xie.system.domain.admin.vo.AdminUserVO;
import com.xie.system.infrastructure.config.SecurityProperties;
import com.xie.system.infrastructure.constants.AuthConstants;
import com.xie.system.infrastructure.security.LoginUserType;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 解析token 放入用户上下文
 */
@Component
@RequiredArgsConstructor
public class AdminUserContextAware implements UserContextAware {

  private final SecurityProperties securityProperties;

  private final IAdminUserService adminUserService;

  @Override
  public BaseJwtUser processToken(String token) {
    boolean pass = JWTUtil.verify(token, securityProperties.getSecret().getBytes(StandardCharsets.UTF_8));
    if(!pass){
      throw new CustomAuthenticationException(AuthErrorMsg.tokenIllegal.getCode(),AuthErrorMsg.tokenIllegal.getName());
    }
    JWT jwt = JWTUtil.parseToken(token);
    Long userId = Long.valueOf(jwt.getPayload().getClaim(AuthConstants.ID).toString());
    LoginUserType type = LoginUserType.of(Integer.valueOf(jwt.getPayload().getClaim(AuthConstants.LOGIN_USER_TYPE).toString())).get();
    if(Objects.equals(LoginUserType.ADMIN_USER,type)){
      //如果是后台用户，查询用户信息，并授权
      AdminUserVO vo = adminUserService.findById(userId);
      SystemJwtUser systemJwtUser = new SystemJwtUser();
      systemJwtUser.setUsername(vo.getUsername());

      return systemJwtUser;
    }
    return new DummyJwtUser();
  }
}
